﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mesoBoard.Data.Repositories;

namespace mesoBoard.Data
{
    public partial class _Forum : Forum
    {
        public int GetPermissionValue(User TheUser, string Action)
        {
            IEnumerable<ForumPermission> Perms = this.ForumPermissions;
            if (TheUser == null)
            {
                ForumPermission tPerm = Perms.FirstOrDefault(x => x.Role.Name == "Guest");
                if (tPerm != null)
                {
                    switch (Action)
                    {
                        case "Posting":
                            return tPerm.Posting;
                        case "Polling":
                            return tPerm.Polling;
                        case "Visiblity":
                            return tPerm.Visibility;
                    }
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                int Posting = 0;
                int Polling = 0;
                int Attachment = 0;
                foreach (InRole r in TheUser.InRoles)
                {
                    if (r.Role.Name == "Admin") return 10;
                    else if (r.Role.Name == "Moderator") return 5;
                    ForumPermission tPerm = Perms.FirstOrDefault(x => x.RoleID == r.RoleID);
                    if (tPerm != null)
                    {

                        switch (Action)
                        {
                            case "Posting":
                                Posting = Posting > tPerm.Posting ? Posting : tPerm.Posting;
                                break;
                            case "Polling":
                                Polling = Polling > tPerm.Polling ? Polling : tPerm.Polling;
                                break;
                            case "Attachment":
                                Attachment = Attachment > tPerm.Attachments ? Attachment : tPerm.Attachments;
                                break;
                            case "Visiblity":
                                return tPerm.Visibility;
                        }
                    }
                }
                if (Action == "Posting")
                    return Posting;
                else if (Action == "Polling")
                    return Polling;
                else if (Action == "Attachment")
                    return Attachment;
            }
            return 0;
        }

        private bool HasPermissionFor(User TheUser, string Action)
        {
            IEnumerable<ForumPermission> Perms = this.ForumPermissions;
            if (TheUser == null)
            {
                ForumPermission tPerm = Perms.FirstOrDefault(x => x.Role.Name == "Guest");
                if (tPerm != null)
                {
                    /*
                    switch (Action)
                    {
                        case "View": if (tPerm.Visibility > 0) return true;
                            break;
                        case "Reply": if (tPerm.Posting >= 1) return true;
                            break;
                        case "CreateThread": if (tPerm.Posting >= 2) return true;
                            break;
                    }
                     */
                    if (tPerm.Visibility > 0) return true;
                }
            }
            else
            {
                foreach (InRole r in TheUser.InRoles)
                { 
                    if (r.Role.Name == "Admin") return true;
                    else if (r.Role.Name == "Moderator") return true;
                    ForumPermission tPerm = Perms.FirstOrDefault(x => x.RoleID == r.RoleID);
                    if (tPerm != null)
                    {
                        switch (Action)
                        {
                            case "View": if (tPerm.Visibility > 0) return true;
                                break;
                            case "Reply": if (tPerm.Posting >= 1) return true;
                                break;
                            case "CreateThread": if (tPerm.Posting >= 2) return true;
                                break;
                            case "CastVote": if (tPerm.Polling > 0) return true;
                                break;
                            case "CreatePoll": if (tPerm.Polling > 1) return true;
                                break;
                            case "DownloadAttachment": if (tPerm.Attachments > 0) return true;
                                break;
                            case "CreateAttachment": if (tPerm.Attachments > 1) return true;
                                break;
                        }
                    }
                }
            }

            return false;
        }

        public bool CanView(User TheUser)
        {
            return HasPermissionFor(TheUser, "View");
        }

        public bool CanReply(User TheUser)
        {
            return HasPermissionFor(TheUser, "Reply");
        }

        public bool CanCreateThread(User TheUser)
        {
            return HasPermissionFor(TheUser, "CreateThread");
        }

        public bool CanCreatePoll(User TheUser)
        {
            return HasPermissionFor(TheUser, "CreatePoll");
        }

        public bool CanCastVote(User TheUser)
        {
            return HasPermissionFor(TheUser, "CastVote");
        }

        public bool CanCreateAttachment(User TheUser)
        {
            return HasPermissionFor(TheUser, "CreateAttachment");
        }

        public bool CanDownloadAttachment(User TheUser)
        {
            return HasPermissionFor(TheUser, "DownloadAttachment");
        }

        public bool HasNewPost(User TheUser)
        {
            if (TheUser == null)
                return false;

            ThreadRepository threads = new ThreadRepository(new mbEntities());
            IEnumerable<Thread> allThreads = threads.GetThreads(this.ForumID);
            return false;
            //return allThreads.Any(x => x.HasNewPost(TheUser));
        }
    }
}
